#!/bin/sh

###################################################
#                                                 #
# site-gen - Mango CMS site generator script      #
#                                                 #
# This script is partly based on seam-gen shipped #
# with any JBoss Seam distribution.               #
#                                                 #
###################################################

##
## Read the configuration
##
. ./site-gen.conf

##
## Prepare some variables
##
CURRENT_DIR=$PWD
SITE_ID=$1
SITE_DIR="${SITE_ROOT}/${SITE_ID}"
SOURCE_LIB_DIR="${CURRENT_DIR}/lib"
TEMPLATE_DIR="${CURRENT_DIR}/templates"

##
## Validate that Java is properly installed
##
if [ -z "$JAVA_HOME" ]; then
	echo "The JAVA_HOME environment variable is not set"
	echo "Please point it to a JDK installation"
	exit 1
elif [ ! -x "$JAVA_HOME/bin/javac" ]; then 
	echo "The JAVA_HOME environment variable should point to a JDK, not a JRE"
	exit 1
fi

##
## Validate input
##
if [ "${SITE_ID}" = "" ]; then
	echo "Usage: site-gen <site_id>"
	exit 1
fi

##
## Aquire some user input
##
echo -n "Database server hostname: "
read DB_HOSTNAME
echo -n "Database server username: "
read DB_USERNAME
echo -n "Database server password: "
read DB_PASSWORD
echo -n "Database name: "
read DB_NAME

##
## Dump some status information
##
echo "SITE ID: ${SITE_ID}"
echo "SITE DIR: ${SITE_DIR}"

## REMOVE ME
rm -rf "${SITE_DIR}"

##
## Prepare the execution of the site generator
##

# Write temporary settings into a Ant property file
touch build.properties
echo "site.name=${SITE_ID}" >> build.properties
echo "site.home=${SITE_DIR}" >> build.properties
echo "source.lib.dir=${SOURCE_LIB_DIR}" >> build.properties
echo "template.dir=${TEMPLATE_DIR}" >> build.properties
echo "db.host=${DB_HOSTNAME}" >> build.properties
echo "db.user=${DB_USERNAME}" >> build.properties
echo "db.pass=${DB_PASSWORD}" >> build.properties
echo "db.name=${DB_NAME}" >> build.properties

# Create the site directory if it doesn't exist, or break out with an error if it does
if [ ! -d "${SITE_DIR}" ]; then
	mkdir -p "${SITE_DIR}"
else
	echo "Site already exists in directory ${SITE_DIR}, unable to continue"
	exit 1
fi

cygwin=false
case "`uname`" in
	CYGWIN*)
		cygwin=true
	;;
esac

if $cygwin; then
	# Must pass real Windows paths to Java (Java doesn't execute in the context of cgywin)
	JAVA_HOME_WINPATH=`cygpath -aw "$JAVA_HOME"`
	CURRENT_DIR_WINPATH=`cygpath -aq "$CURRENT_DIR"`
	"$JAVA_HOME/bin/java" -cp "$JAVA_HOME_WINPATH\lib\tools.jar;$CURRENT_DIR_WINPATH\build\lib\ant-launcher.jar;$CURRENT_DIR_WINPATH\build\lib\ant-nodeps.jar;$CURRENT_DIR_WINPATH\build\lib\ant.jar;$CURRENT_DIR_WINPATH\build\lib\maven-ant-tasks.jar" -Dant.home="$CURRENT_DIR_WINPATH\lib" org.apache.tools.ant.launch.Launcher -buildfile "$CURRENT_DIR_WINPATH\build\build.xml" -Dworking.dir="$CURRENT_DIR_WINPATH"
else
	"$JAVA_HOME/bin/java" -cp "$JAVA_HOME/lib/tools.jar:$CURRENT_DIR/build/lib/ant-launcher.jar:$CURRENT_DIR/build/lib/ant-nodeps.jar:$CURRENT_DIR/build/lib/ant.jar:$CURRENT_DIR/build/lib/maven-ant-tasks.jar" -Dant.home="$CURRENT_DIR/lib" org.apache.tools.ant.launch.Launcher -buildfile "$CURRENT_DIR/build/build.xml" -Dworking.dir="$CURRENT_DIR"
fi

# Remove the temporary Ant property file
rm -f build.properties
